package cn.edu.scau.cmi.liangzaoqing.fxml;

import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;

import org.apache.metamodel.schema.Schema;
import cn.edu.scau.cmi.liangzaoqing.util.ObtainDataBaseMetadataUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class DatabaseModelInfoController implements Initializable {

	private Map<String, String> databaseInfo;

	@FXML
	private Button nextButton;

	@FXML
	private TextField projectTextField;

	@FXML
	private TextField packageTextField;

	@FXML
	private TextField ipTextField;

	@FXML
	private TextField usernameTextField;

	@FXML
	private TextField passwordTextField;

	@FXML
	private TextField portTextField;

	@FXML
	private ChoiceBox<String> frameworkChoiceBox;

	@FXML
	private ChoiceBox<String> databaseTypeChoiceBox;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub
		initializeChoiceBox();
		initializeTextField();
	}

	public void initializeTextField() {
		projectTextField.textProperty().addListener((observable, oldValue, newValue) -> {
			packageTextField.setText("cn.edu.scau.cmi."+newValue.toLowerCase());
		});
		ipTextField.setText("127.0.0.1");
		portTextField.setText("3306");
		usernameTextField.setText("root");
		passwordTextField.setText("root");
	}

	public void initializeChoiceBox() {
		ObservableList<String> frameworkOptions = FXCollections.observableArrayList("SSH", "SSM", "Android", "Wechat",
				"SpringBoot");
		ObservableList<String> databaseTypeOptions = FXCollections.observableArrayList("Mysql", "Oracle", "Access",
				"DB2", "SQL Server", "SyBase");

		frameworkChoiceBox.setValue("SSH"); // this statement shows default value
		frameworkChoiceBox.setItems(frameworkOptions); // this statement adds all values in choiceBox

		databaseTypeChoiceBox.setValue("Mysql"); // this statement shows default value
		databaseTypeChoiceBox.setItems(databaseTypeOptions); // this statement adds all values in choiceBox
	}

	// 链接数据库，获取所有的数据库名
	public Schema[] getAllSchemas() throws ClassNotFoundException, SQLException {
		ObtainDataBaseMetadataUtil DatabaseSchema = new ObtainDataBaseMetadataUtil();
		Schema[] schemas = DatabaseSchema.getAllSchemas(databaseTypeChoiceBox.getValue().toString(),
				ipTextField.getText(), portTextField.getText(), usernameTextField.getText(),
				passwordTextField.getText());
		return schemas;
	}

	public void nextStep() throws ClassNotFoundException, SQLException, IOException {

		initializeDataBaseInfoMap();
		
		Stage nextStage = new Stage();
		nextStage.setTitle("数据库物理模型创建应用");
		FXMLLoader loader = new FXMLLoader(getClass().getResource("SchemaInfoPage.fxml"));
		Pane nextPane = loader.load();
		SchemaInfoController schemaInfoController = (SchemaInfoController) loader.getController();
		schemaInfoController.initializeSchema(getAllSchemas());
		schemaInfoController.setDatabaseInfo(databaseInfo);
		Scene nextScene = new Scene(nextPane);
		nextStage.setScene(nextScene);
		nextStage.setResizable(false);
		nextStage.show();
		Stage stage = (Stage) nextButton.getScene().getWindow();
		stage.close();
	}

	public void initializeDataBaseInfoMap() {
		databaseInfo = new HashMap<>();
		databaseInfo.put("project", projectTextField.getText());
		databaseInfo.put("package", packageTextField.getText());
		databaseInfo.put("framework", frameworkChoiceBox.getValue());
		databaseInfo.put("databaseType", databaseTypeChoiceBox.getValue());
		databaseInfo.put("ip", ipTextField.getText());
		databaseInfo.put("port", portTextField.getText());
		databaseInfo.put("username", usernameTextField.getText());
		databaseInfo.put("password", passwordTextField.getText());
	}

}
